/*
 * GoodsDetailInfoPresenter.java
 * business
 *
 * Created by SunDing on 2016/9/28.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.goodsdetail.value;

import com.yidiandao.business.data.model.GoodsModel;
import com.yidiandao.business.data.model.IGoodsModel;
import com.yidiandao.business.data.response.GoodsDetailValueResponse;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.ArrayList;
import java.util.List;

/** 商品推荐 */
public class GoodsDetailValuePresenter implements GoodsDetailValueContract.Presenter {

    private final IGoodsModel mModel;
    private GoodsDetailValueContract.View mView;
    private int currentPage = 1;

    private int totalPage = 1;

    private List<GoodsDetailValueResponse.ValueBean> mList = new ArrayList<>();

    private boolean isLoading;

    public GoodsDetailValuePresenter(GoodsDetailValueContract.View view) {
        mView = view;
        mView.setPresenter(this);
        mModel = new GoodsModel();
    }

    @Override
    public void start() {
    }

    @Override
    public void loadMore(String goods_id, int type, final boolean isRefresh) {
        if (isRefresh) {
            currentPage = 1;
            totalPage = 1;
        }
        if (currentPage > totalPage || isLoading) {
            //没有更多数据了
            return;
        }
        isLoading = true;
        mModel.getValueData(
                mView.getPagerTag(),
                goods_id,
                type,
                currentPage,
                new IGoodsModel.onGetDataListener() {
                    @Override
                    public void onFailed() {
                        if (isRefresh) {
                            mView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                        } else {
                            mView.loadMoreError();
                        }
                    }

                    @Override
                    public void onFinal() {
                        isLoading = false;
                    }

                    @Override
                    public void onSuccess(
                            GoodsDetailValueResponse response,
                            List<GoodsDetailValueResponse.ValueBean> data) {
                        if (isRefresh) {
                            mList.clear();
                            mView.refreshContent(mView.checkResultObject(data));
                        }
                        totalPage = response.getPagination().getTotal();
                        currentPage++;
                        if (data != null) {
                            mList.addAll(data);
                            mView.setGoodsList(mList);
                        }
                        mView.loadMoreFinish(data.size() == 0, currentPage <= totalPage);
                        String allCount = response.getData().getAllCount();
                        String niceCount = response.getData().getNiceCount();
                        String mediumCount = response.getData().getMediumCount();
                        String badCount = response.getData().getBadCount();
                        String baskCount = response.getData().getBaskCount();
                        mView.setTitleBarData(
                                allCount, niceCount, mediumCount, badCount, baskCount);
                    }
                });
    }
}
